package com.kotlinvben.entity

import com.fasterxml.jackson.annotation.JsonFormat
import com.kotlinvben.enumeration.Status
import jakarta.persistence.*
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.DynamicUpdate
import org.hibernate.annotations.SoftDelete
import org.hibernate.annotations.UpdateTimestamp
import java.time.LocalDateTime

/**
 * @author wxluo2
 * @date 2025/5/24 21:31
 */
@Entity
@Table(name = "roles")
@SoftDelete(columnName = "deleted")
@DynamicUpdate
data class Role(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long? = null,

    @Column(nullable = false, length = 50)
    var name: String,

    @Column(nullable = false)
    var code: String,

    @Column(nullable = false)
    var status: Int = Status.ENABLED.code,

    @Column(nullable = false, insertable = false, updatable = false)
    val deleted: Boolean = false,

    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    var createTime: LocalDateTime? = null,

    @Column(nullable = false, length = 50)
    var createBy: String? = null,

    @UpdateTimestamp
    @Column(nullable = false)
    var modifyTime: LocalDateTime? = null,

    @Column(nullable = false, length = 50)
    var modifyBy: String? = null,

    var remark: String? = null,

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(
        name = "role_menus",
        joinColumns = [JoinColumn(name = "role_id")],
        inverseJoinColumns = [JoinColumn(name = "menu_id")]
    )
    var menus: MutableList<Menu> = mutableListOf(),

    @Transient
    var permissions: List<Long>? = null,
)
